home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / TouchMe 1.2□ / touchMe 1.2 Folder / touchMe source codes / CW11 PP source / source / CTouchMeApp.cp next >
Encoding:
Text File  |  1997-04-25  |  15.1 KB  |  601 lines  |  [TEXT/CWIE]

  1. // ==================================================
  2. //    CTouchMeApp.cp
  3. //    Copyright (C) 1996-1997 Mizutori Tetsuya
  4. //    July 4 ,1996; August 4, 1996; February 3, 1997; April 23, 1997.
  5. // ==================================================
  6. //    All documents are pretty-printed in 10-point Geneva font.
  7.  
  8. #include <TextUtils.h>
  9. //#include <Balloons.h>
  10.  
  11. #include <LGrowZone.h>
  12. #include <LWindow.h>
  13. #include <LDialogBox.h>
  14. #include <LString.h>
  15. //#include <UModalDialogs.h>
  16. #include <UDrawingState.h>
  17. #include <UMemoryMgr.h>
  18. #include <URegistrar.h>
  19. #include <UDesktop.h>
  20. #include <UEnvironment.h>
  21.  
  22. #include <PP_Messages.h>
  23. #include <PP_Resources.h>
  24. #include <PPobClasses.h>
  25.  
  26. #include "touchMeConstants.h"
  27. #include "touchMeRegistry.h"
  28. #include "CTouchMeAppleEvents.h"
  29. #include "CTouchMeApp.h"
  30. #include "CTouchMePref.h"
  31. #include "CAppleGuideFile.h"
  32. #include "CRadioButton.h"
  33. #include "CDateEditField.h"
  34. #include "CTouchMeMainWindow.h"
  35. #include "UFileTools.h"
  36. #include "UFinderEvents.h"
  37. #include "UStandardFiles.h"
  38. #include "UMacOSTools.h"
  39. #include "UErrorMessage.h"
  40.  
  41.  
  42. // Patch for displaying a colored alert dialog. Thanks to Rokkaku Fumio.
  43. #define PATCH_ACTB TRUE
  44.  
  45. ModalFilterUPP    CTouchMeApp::sAlertPatchProc    = NULL;
  46.  
  47.  
  48. // --------------------------------------------------
  49. //        • Main Program
  50. // --------------------------------------------------
  51.  
  52. void main( void )
  53. {
  54.     // Setup the throw and signal actions.
  55.     SetDebugThrow_( debugAction_Alert );
  56.     SetDebugSignal_( debugAction_Alert );
  57.  
  58.     // Initialize the heap. Parameter is number of master handle blocks to allocate.
  59.     InitializeHeap( 3 );
  60.  
  61.     // Initialize the MacOS toolbox.
  62.     UQDGlobals::InitializeToolbox( &qd );
  63.  
  64.     // Install a GrowZone function to catch  low memory situations.
  65.     // Parameter is the size of the memory reserve in bytes.
  66.     new LGrowZone( 20000 );
  67.  
  68.     CTouchMeApp    theApp;
  69.     theApp.Run();
  70. }
  71.  
  72.  
  73. // --------------------------------------------------
  74. //        • CTouchMeApp
  75. // --------------------------------------------------
  76. //    Constructor
  77.  
  78. CTouchMeApp::CTouchMeApp()
  79. {
  80.     // Register functions to create core PowerPlant classes.
  81. //    RegisterAllPPClasses();
  82.     RegisterPainClasses();
  83.  
  84.     mMainWindow = NULL;
  85.  
  86.     // Create a stack array to hold the file specifications.
  87.     mFileArray = (LArray *) new LArray( sizeof( FSSpec ) );
  88.     ThrowIfNil_( mFileArray );
  89.  
  90.     // Create a preferences class object corresponding to "touchMe Prefs" file.
  91.     Str255    theFilename;
  92.     ::GetIndString( theFilename, rSTRx_TouchMe, rSTRx_TouchMe_PrefFile );
  93.     mPref = (CTouchMePref *) new CTouchMePref( theFilename );
  94.     Assert_( mPref != NULL );
  95.  
  96.     // Initialize the preferences reading from preferences file, or by default value.
  97.     mPref->LoadPrefsData();
  98.  
  99.     // Prepare Apple Guide file.
  100.     mAppleGuideFile = (CAppleGuideFile *) new CAppleGuideFile( kThisApplicationCreatorType );
  101.     Assert_( mAppleGuideFile != NULL );
  102.  
  103.     // Most of these flags are reset every time at idle time operation 'UseIdleTime()'.
  104.     // Setup my program process status.
  105.     mOpenApplication = false;
  106.     mOpenDocument = false;
  107.     mKeyModifier = false;
  108.     mUpdatePref = false;
  109.  
  110.     // Reset the document counter.
  111.     mCount = 0;
  112.  
  113.     // Check if cmd-period key is pressed.
  114.     mIsCommandPeriod = false;
  115.  
  116. #ifdef PATCH_ACTB
  117.     sAlertPatchProc = NewModalFilterProc( MyAlertPatch );
  118. #endif // PATCH_ACTB
  119. }
  120.  
  121.  
  122. // --------------------------------------------------
  123. //        • ~CTouchMeApp
  124. // --------------------------------------------------
  125. //    Destructor
  126.  
  127. CTouchMeApp::~CTouchMeApp()
  128. {
  129.     if ( mFileArray != NULL ) delete mFileArray;
  130.  
  131.     if ( mPref != NULL ) delete mPref;
  132.  
  133.     if ( mAppleGuideFile != NULL ) delete mAppleGuideFile;
  134.  
  135. #ifdef PATCH_ACTB
  136.  
  137.     if ( sAlertPatchProc != NULL ) {
  138.         DisposeRoutineDescriptor( sAlertPatchProc );
  139.     }
  140.     sAlertPatchProc = NULL;
  141.  
  142. #endif // PATCH_ACTB
  143. }
  144.  
  145.  
  146. // --------------------------------------------------
  147. //        • StartUp
  148. // --------------------------------------------------
  149.  
  150. void
  151. CTouchMeApp::StartUp()
  152. {
  153.     // Setup my program process status.
  154.     mOpenApplication = true;
  155.  
  156.     // Let's create a main window.
  157.     if ( mMainWindow == NULL ) {
  158.         mMainWindow = MakeMainWindow();
  159.     } else {
  160.         ((CTouchMeMainWindow *) mMainWindow)->Show();
  161.     }
  162. }
  163.  
  164.  
  165. // --------------------------------------------------
  166. //        • OpenDocument
  167. // --------------------------------------------------
  168.  
  169. void
  170. CTouchMeApp::OpenDocument(
  171.     FSSpec *        inMacFSSpec )
  172. {
  173.     // Setup my program process status.
  174.     mOpenDocument = true;
  175.  
  176.     // If cmd-period key was pressed during operation, then terminate the operation.
  177.     if ( mIsCommandPeriod || UMacOSTools::IsCommandPeriod() ) {
  178.         mIsCommandPeriod = true;
  179.         return;
  180.     }
  181.  
  182.     // All documents are to be collected into the file array stack.
  183.     if ( mFileArray == NULL ) return;
  184.  
  185.     // Read the search level from the settings of main window if necessary.
  186.     if ( mUpdatePref &&  mMainWindow != NULL ) {
  187.         mPref->GetPrefsFromWindow( mMainWindow );
  188.         mUpdatePref = false;
  189.     }
  190.  
  191.     // Put it on the file array stack.
  192.     mFileArray->InsertItemsAt( 1, LArray::index_Last, (void *) inMacFSSpec );
  193.     mCount += 1;
  194. }
  195.  
  196.  
  197. // --------------------------------------------------
  198. //        • ChooseDocument
  199. // --------------------------------------------------
  200.  
  201. void
  202. CTouchMeApp::ChooseDocument()
  203. {
  204.     // Deactivate the desktop.
  205.     UDesktop::Deactivate();
  206.  
  207.     // Browse for a document.
  208.     SFTypeList            theTypeList;    // = {'TEXT', 'JPEG'};
  209.     StandardFileReply    theReply;
  210. //    ::StandardGetFile( NULL, -1, theTypeList, &theReply );
  211.     UStandardFiles::StandardGetFileOrDirectory( NULL, -1, theTypeList, &theReply );
  212. //    UStandardFiles::StandardGetFileOrDirectory( NULL, 2, theTypeList, &theReply );
  213.  
  214.     // Activate the desktop.
  215.     UDesktop::Activate();
  216.  
  217. #ifdef COMMENT
  218.     if ( theReply.sfGood ) {
  219.         SendAEOpenDoc( theReply.sfFile );
  220.         if ( theReply.sfIsFolder || theReply.sfIsVolume ) {
  221.             // If the choosen FSSpec is a directory, then servey all files in it.
  222.             SurveyFiles( searchLevel_one, theReply.sfFile );
  223.         } else {
  224.             // If the choosen FSSpec is a plain file, then push it on the file array stack.
  225.             SendAEOpenDoc( theReply.sfFile );
  226.         }
  227.     }
  228. #endif // COMMENT
  229.  
  230.     // Send an apple event to open the file.
  231.     if ( theReply.sfGood ) SendAEOpenDoc( theReply.sfFile );
  232.  
  233. }
  234.  
  235.  
  236. // --------------------------------------------------
  237. //        • ObeyCommand
  238. // --------------------------------------------------
  239. //    Respond to commands
  240.  
  241. Boolean
  242. CTouchMeApp::ObeyCommand(
  243.     CommandT        inCommand,
  244.     void *        ioParam)
  245. {
  246.     Boolean    cmdHandled = true;
  247.  
  248.     switch ( inCommand ) {
  249.         case msg_Main_ButtonOK:
  250.         case msg_Main_ButtonCencel:
  251.         {
  252.             delete mPref;
  253.             if ( mMainWindow != NULL ) delete (CTouchMeMainWindow *) mMainWindow;
  254.  
  255.             SendAEQuit();
  256.         }
  257.         break;
  258.  
  259.         case cmd_Save:
  260.         case msg_SavePrefs:
  261.         {
  262.             // Save all the settings to the preferences file.
  263.             if ( mMainWindow != NULL ) mPref->GetPrefsFromWindow( mMainWindow );
  264.             mPref->SavePrefsData();
  265.         }
  266.         break;
  267.  
  268.         case msg_SavePrefsWFrame:
  269.         {
  270.             // Save the window position only, but not all the other settings.
  271.             if ( mMainWindow != NULL ) mPref->GetPrefsFromWindow( mMainWindow );
  272.  
  273.             Rect        theRect;
  274.             mPref->GetWindowRect( theRect );
  275.             // Revert the settings to read from preferences file, and set the window position.
  276.             mPref->LoadPrefsData();
  277.             mPref->SetWindowRect( theRect );
  278.             mPref->SavePrefsData();
  279.         }
  280.         break;
  281.  
  282.         case cmd_Close:
  283.         {
  284.             // Do nothing here.
  285.         }
  286.         break;
  287.  
  288.         case msg_Help:
  289.         {
  290.             // Open Apple Guide file.
  291.             mAppleGuideFile->Open();
  292.         }
  293.         break;
  294.  
  295.         case msg_Touch:
  296.         {
  297.             // Touch files. Process:SendAEOpenDoc() -> HandleDocuments().
  298.             FSSpec    * theFSSpec = (FSSpec *) ioParam;
  299.             SendAEOpenDoc( *theFSSpec );
  300.         }
  301.         break;
  302.  
  303.         default:
  304.         {
  305.             cmdHandled = LDocApplication::ObeyCommand( inCommand, ioParam );
  306.         }
  307.         break;
  308.     }
  309.  
  310.     return cmdHandled;
  311. }
  312.  
  313.  
  314. // --------------------------------------------------
  315. //        • FindCommandStatus
  316. // --------------------------------------------------
  317.  
  318. void
  319. CTouchMeApp::FindCommandStatus(
  320.     CommandT        inCommand,
  321.     Boolean &        outEnabled,
  322.     Boolean &        outUsesMark,
  323.     Char16 &        outMark,
  324.     Str255        outName )
  325. {
  326.     switch ( inCommand ) {
  327.         case cmd_About:
  328.         case cmd_Open:
  329.         case cmd_Close:
  330.         case cmd_Quit:
  331.             outEnabled = true;
  332.             break;
  333.         case cmd_New:
  334.         case cmd_Save:
  335.         case cmd_PageSetup:
  336.             outEnabled = false;
  337.             break;
  338.         default:
  339.             LDocApplication::FindCommandStatus( inCommand,
  340.                 outEnabled, outUsesMark, outMark, outName );
  341.             break;
  342.     }
  343. }
  344.  
  345.  
  346. // --------------------------------------------------
  347. //        • UseIdleTime
  348. // --------------------------------------------------
  349.  
  350. void
  351. CTouchMeApp::UseIdleTime(
  352.     const EventRecord &    inMacEvent )
  353. {
  354.     LDocApplication::UseIdleTime( inMacEvent );
  355.  
  356.     if ( inMacEvent.what == nullEvent ) {
  357.         // Check the modifier key when opening the documents.
  358. //        mKeyModifier = ( (inMacEvent.modifiers & ( optionKey | controlKey) ) != 0 );
  359.         mKeyModifier = UMacOSTools::IsModifierKeyPressed( inMacEvent, optionKey | controlKey );
  360.  
  361.         // If the application was invoked by drag and drop, the main window is not displayed.
  362.         if ( mMainWindow != NULL ) {
  363.             ((CTouchMeMainWindow *) mMainWindow)->Indicator( mKeyModifier );
  364.         }
  365.     }
  366.  
  367.     // Reset counter for the incoming document files.
  368.     mCount = 0;
  369.  
  370.     // Each time before a series of operations, prefs data must be updated.
  371.     mUpdatePref = true;
  372.  
  373.     // Do with the files stacked in the file array.
  374.     HandleDocuments();
  375.  
  376.     // Here, flag of cmd-period pressing is cleared every once idle time begins.
  377.     mIsCommandPeriod = false;
  378.  
  379.     // To quit immediately after the drag&drop operation finished.
  380.     // Do not quit when you have opened the application by a double-click.
  381.     if ( ! mOpenApplication  && mOpenDocument )  SendAEQuit();
  382. }
  383.  
  384.  
  385. // --------------------------------------------------
  386. //        • ShowAboutBox
  387. // --------------------------------------------------
  388.  
  389. void
  390. CTouchMeApp::ShowAboutBox()
  391. {
  392. #ifdef PATCH_ACTB
  393.  
  394.     UDesktop::Deactivate();
  395.     ::Alert( ALRT_About, sAlertPatchProc );
  396.     UDesktop::Activate();
  397.  
  398. #else // PATCH_ACTB
  399.  
  400.     LDocApplication::ShowAboutBox();
  401.  
  402. #endif // PATCH_ACTB
  403. }
  404.  
  405.  
  406. #ifdef PATCH_ACTB
  407. // --------------------------------------------------
  408. //        • MyAlertPatch
  409. // --------------------------------------------------
  410. // Patch for the colored alert dialog. Thanks to Rokkaku Fumio.
  411.  
  412. #include  <UKeyFilters.h>
  413.  
  414. pascal Boolean
  415. CTouchMeApp::MyAlertPatch(
  416.     DialogRef        theDialog,
  417.     EventRecord *    theEvent,
  418.     short *        itemHit )
  419. {
  420.     Boolean        eventOccurred = false;
  421.  
  422.     switch ( theEvent->what ) {
  423.         case keyDown:
  424.         {
  425.             short    charCode = (theEvent->message & charCodeMask);
  426. //            if ( (charCode ==kEnterKey ) || (charCode ==kReturnKey) ) {
  427.             if ( UKeyFilters::IsActionKey( charCode ) ) {
  428.                 ControlRef    aButton;
  429.                 Rect        aRect;
  430.                 short        aType;
  431.                 long        finalTick;
  432.  
  433.                 ::GetDialogItem( theDialog, ok, &aType, (Handle *)&aButton, &aRect );
  434.                 ::HiliteControl( aButton, kControlButtonPart );
  435.                 ::Delay( 8, &finalTick );
  436.                 ::HiliteControl( aButton, kControlNoPart );
  437.                 *itemHit = kStdOkItemIndex;
  438.                 eventOccurred = true;
  439.             }
  440.         }
  441.         break;
  442.  
  443.         case updateEvt:
  444.         {
  445.             WindowRef        theWindow = ::GetDialogWindow( theDialog );
  446.             if ( (WindowRef)theEvent->message == theWindow ) {
  447.                 ::SelectWindow( theWindow );
  448.             }
  449.         }
  450.         break;
  451.     }
  452.     return eventOccurred;
  453. }
  454. #endif // PATCH_ACTB
  455.  
  456.  
  457. // ==================================================
  458. //    Application-spicific functions
  459. // ==================================================
  460.  
  461. // --------------------------------------------------
  462. //        • MakeMainWindow
  463. // --------------------------------------------------
  464.  
  465. LWindow *
  466. CTouchMeApp::MakeMainWindow()
  467. {
  468.     LWindow *        theWindow;
  469.  
  470.     // Create the window.
  471.     theWindow = CTouchMeMainWindow::CreateWindow( rPPob_TouchMeMainWindow, this );
  472.     Assert_( theWindow != NULL );
  473.  
  474.     // Set the settings of the window.
  475.     mPref->SetPrefsToWindow( theWindow );
  476.  
  477.     // Show the window.
  478.     theWindow->Show();
  479.  
  480.     return theWindow;
  481. }
  482.  
  483.  
  484. // --------------------------------------------------
  485. //        • HandleDocuments
  486. // --------------------------------------------------
  487. // This procedure is the major part of touchMe program.
  488. // If option-key is pressed, it works as a 'fetch' command.
  489. // If not, it works as a 'touch' command regularly.
  490.  
  491. void
  492. CTouchMeApp::HandleDocuments( void )
  493. {
  494.     if ( mFileArray == NULL ) return;
  495.  
  496.     // Let's retrieve all the files stored in the file array.
  497.     unsigned long    theItemCount = mFileArray->GetCount();
  498.     if ( theItemCount == 0 ) return;
  499.  
  500.     // Load settings from main window if necessary.
  501.     if ( mUpdatePref && mMainWindow != NULL ) {
  502.         mPref->GetPrefsFromWindow( mMainWindow );
  503.         mUpdatePref = false;
  504.     }
  505.  
  506.     ETouchFlag    flagCr        = mPref->GetFlagNumb( touchType_CreationDate );
  507.     ETouchFlag    flagMd        = mPref->GetFlagNumb( touchType_ModificationDate );
  508.     Boolean    toUpdateCr        = mPref->GetEnabled( touchType_CreationDate );
  509.     Boolean    toUpdateMd        = mPref->GetEnabled( touchType_ModificationDate );
  510.     Boolean    toQuit        = false;
  511.  
  512.     unsigned long    theCrDateTime = 0, theMdDateTime = 0;
  513.     unsigned long    theDateTimeSecs;
  514.     ::GetDateTime( &theDateTimeSecs );    // First, set the current date time.
  515.  
  516.     // Try to handle each file.
  517.     for ( ArrayIndexT index = LArray::index_First; (index <= theItemCount) && ! toQuit; index++ ) {
  518.  
  519.         // Pressing cmd-period terminates the execution.
  520.         if ( mIsCommandPeriod || UMacOSTools::IsCommandPeriod() ) {
  521.             mIsCommandPeriod = true;
  522.             break;
  523.         }
  524.  
  525.         FSSpec    theFSSpec;
  526.         mFileArray->FetchItemAt( index, &theFSSpec );
  527.  
  528.         // Check the modifier key whether or not the option-key is pressed.
  529.         // If option-key is pressed, it works as a 'fetch' command and exits immediately.
  530.         if ( mKeyModifier ) {
  531.             if ( mMainWindow != NULL )
  532.                 ((CTouchMeMainWindow *)mMainWindow)->BroadcastMessage( msg_Main_DroppedFile, (void *)&theFSSpec );
  533.             break;
  534.         }
  535.  
  536.         // Do special case if the item is the first one.
  537.         if ( index == LArray::index_First )  {
  538.  
  539.             // Fetch the date time stamp from the first dropped file.
  540.             UFileTools::GetFSSpecDateTime( theFSSpec, theCrDateTime, theMdDateTime );
  541.  
  542.             // Creation date:
  543.             // Setup the date time stamp according to the flag of settings.
  544.             switch ( flagCr ) {
  545.                 case touchFlag_Current:
  546.                     theCrDateTime = theDateTimeSecs;
  547.                     break;
  548.                 case touchFlag_Direct:
  549.                     theCrDateTime = mPref->GetDateTime( touchType_CreationDate );
  550.                     break;
  551.                 case touchFlag_First:
  552.                     break;
  553.                 case touchFlag_Second:
  554.                     mPref->SetDateTime( touchType_CreationDate, theCrDateTime );
  555.                     if ( mMainWindow != NULL ) mPref->SetPrefsToWindow( mMainWindow );
  556.                     break;
  557.             }
  558.  
  559.             // Modification date:
  560.             // Setup the date time stamp according to the flag of settings.
  561.             switch ( flagMd ) {
  562.                 case touchFlag_Current:
  563.                     theMdDateTime = theDateTimeSecs;
  564.                     break;
  565.                 case touchFlag_Direct:
  566.                     theMdDateTime = mPref->GetDateTime( touchType_ModificationDate );
  567.                     break;
  568.                 case touchFlag_First:
  569.                     break;
  570.                 case touchFlag_Second:
  571.                     mPref->SetDateTime( touchType_ModificationDate, theMdDateTime );
  572.                     if ( mMainWindow != NULL ) mPref->SetPrefsToWindow( mMainWindow );
  573.                     break;
  574.             }
  575.  
  576.         }
  577.  
  578.         // If the{Cr,Md}DateTime has a zero value, then its date time of FSSpec wll not be changed.
  579.         if ( ! toUpdateCr )  theCrDateTime = 0;
  580.         if ( ! toUpdateMd )  theMdDateTime = 0;
  581.         UFileTools::SetFSSpecDateTime( theFSSpec, theCrDateTime, theMdDateTime );
  582.  
  583.     }
  584.  
  585.     // Get Info if shift key is pressed.
  586.     if ( UMacOSTools::IsModifierKeyPressed(shiftKey) ) {
  587.         for ( ArrayIndexT index = LArray::index_First; index <= theItemCount; index++ ) {
  588.             FSSpec    theFSSpec;
  589.             mFileArray->FetchItemAt( index, &theFSSpec );
  590.             UFinderEvents::GetInfoSelection( theFSSpec );
  591.         }
  592.     }
  593.  
  594.     // Discard all the files stored in the file array.
  595.     mFileArray->RemoveItemsAt( theItemCount, LArray::index_First );
  596.  
  597. }
  598.  
  599.  
  600. // end of program
  601.